<?php
import("com.simpleApplication.events.BackendEvent");
import("com.simpleApplication.model.tables.Page");
class BackendMediator extends Mediator {
	public function __construct(ITemplate $template){
		parent::__construct($template);
		var_dump($template);
		//var_dump(Yamvc::instnce()->getClassesInClassPath("com.simpleApplication.view"));
		$this->addEventListener(ApplicationEvent::ACTION_DISPATCHED,new EventListener($this,"initalize"));
	}
	
	public function initalize(ApplicationEvent $e){
		$page = new Page();
		echo "name -> ".($page->getByPk(1)->namepage)."<br />";
		if (Application::getAppInstance()->getUser()->isAuthOk()){
			$this->addEventListener(ApplicationEvent::DEFAULT_EVENT,new EventListener($this,"defaultHandler"));
			$this->addEventListener(BackendEvent::ShowProxy,new EventListener($this,"showProxyProps"));
			$this->addEventListener(BackendEvent::Execute,new EventListener($this,"executeProxyMethod"));
			$proxyes = Yamvc::instnce()->getClassesInClassPath("com.simpleApplication.model");
			$this->_template->putVariable("proxyes",$proxyes);
		}else{
			$this->_template->putVariable("auth",View::instance()->getTemplate("auth_panel"));
		}
	}
	
	public function defaultHandler(ApplicationEvent $e){
		$proxyes = Yamvc::instnce()->getClassesInClassPath("com.simpleApplication.model");
		$this->_template->putVariable("proxyes",$proxyes);		
	}
	
	public function showProxyProps(BackendEvent $e){
		try{
			import("com.simpleApplication.model.".$e->getProxyClass());
			$class=$e->getProxyClass();
			$proxy = new $class;
		}catch (ClassNotFoundException $ex){
			echo $ex->getMessage();
			return ;
		}
		$this->_template->putVariable("title",$class);
		$this->_template->putVariable("form",$this->createEditForm($class,$proxy->getBackendConfig()));
	}
	
	public function executeProxyMethod(BackendEvent $e){
		try{
			import("com.simpleApplication.model.".$e->getProxyClass());
			$class=$e->getProxyClass();
			$proxy = new $class;
			$funcName = Request::instance()->post->func;
			$conf=$proxy->getBackendConfig();
			$params = $conf[$funcName];
			var_dump($params);
			$fParams=array();
			foreach ($params as $name=>$value){
				echo $name;
				$fParams[] = Request::instance()->post->$name;
				
			}
			call_user_func_array(array($proxy,$funcName),$fParams);
			//$proxy->{$funcName}();
			Utils::redirect("backend/BackendEvent/showProxy/".$class."");
		}catch (ClassNotFoundException $ex){
			echo $ex->getMessage();
			return ;
		}catch (NullPointerException $ex){
			echo $ex->getMessage();
			return;
		}
	}
	
	private function createEditForm($proxyClass,$params){
		if($params==null||is_array($params)==false){
			return "No any config functions found";
		}
		$form = "<form method='post' action='/backend/BackendEvent/execute/".$proxyClass."/' >";
		foreach ($params as $function=>$args){
			$form.="<h4>".$function."</h4><br />\n";
			$form.="<input type'hidden' value='".$function."' name='func' id='func'/>";
			foreach ($args as $name=>$arg) {
				if (isset($arg['name'])){
					$form.="<span>".$arg["name"]."</span>";
				}
				if (isset($arg['type'])){
					if ($arg['type']=="text"){
						$form.="<input ";
						$form.="id='".$name."' name='".$name."' type='text' />";		
					}elseif ($arg['type']=="textarea"){
						$form.="<textarea ";
						$form.="id='".$name."' name='".$name."' ></textarea>";
					}
				}
				//$form.="id=".$name." type='text' />";
			}
		}
		$form.= "<input type='submit' value='Execute'></form>";	
		return $form;
		
	}
}

?>